// pages/constellation/constellation.js
var Ajax = require('../../utils/Ajax.js')
var utils = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[
      {
        index:1,
        name:'白羊座',
        icon:'icon-icon_-xingzuo-baiyangzuo'
      },
      {
        index: 2,
        name: '金牛座',
        icon: 'icon-icon_-xingzuo-jinniuzuo'
      },
      {
        index: 3,
        name: '双子座',
        icon: 'icon-icon_-xingzuo-shuangzizuo'
      },
      {
        index: 4,
        name: '巨蟹座',
        icon: 'icon-icon_-xingzuo-juxiezuo'
      },
      {
        index: 5,
        name: '狮子座',
        icon: 'icon-icon_-xingzuo-shizizuo'
      },
      {
        index: 6,
        name: '处女座',
        icon: 'icon-icon_-xingzuo-chunvzuo'
      },
      {
        index: 7,
        name: '天秤座',
        icon: 'icon-icon_-xingzuo-tianchengzuo'
      },
      {
        index: 8,
        name: '天蝎座',
        icon: 'icon-icon_-xingzuo-tianxiezuo'
      },
      {
        index: 9,
        name: '射手座',
        icon: 'icon-icon_-xingzuo-sheshouzuo'
      },
      {
        index: 10,
        name: '摩羯座',
        icon: 'icon-icon_-xingzuo-mojiezuo'
      },
      {
        index: 11,
        name: '水瓶座',
        icon: 'icon-icon_-xingzuo-shuipingzuo'
      },
      {
        index: 12,
        name: '双鱼座',
        icon: 'icon-icon_-xingzuo-shuangyuzuo'
      }
    ],
    setType:1,
    info:{},
    isSet:false,
    showMode:false,
    setItem:''
  },
  //弹窗选择星座
  setItem(e){
    var that = this
    var setItem = e.currentTarget.dataset.item
    app.saveSync("constellationType", setItem.index);
    this.setData({
      showMode: !this.data.showMode,
      setType: setItem.index,
      setItem: setItem
    },()=>{
      that.getInfo()
    })
    this.showMode()
    
  },
  //查询当前的星座
  getInfo(){
    var that = this
    utils.showLoading()
    Ajax.post('constellationQuery', {
      name: this.data.setItem.name
    }).then((d) => {
      wx.hideLoading();
      if (d.status == 200) {
        // console.log(d)
        var datainfo = JSON.parse(d.data)
        datainfo['all'] = parseFloat(datainfo['all'].replace("%", "")) 
        datainfo['work'] = parseFloat(datainfo['work'].replace("%", "")) 
        datainfo['love'] = parseFloat(datainfo['love'].replace("%", ""))
        datainfo['money'] = parseFloat(datainfo['money'].replace("%", ""))
        that.setData({
          info: datainfo
        })
        // console.log(this.data.info)
      } else {
        wx.hideLoading();
      }
    }).catch((e) => {
      wx.hideLoading();
      console.log(e)
    })
  },
  showMode(){
    this.setData({
      showMode: !this.data.showMode
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //判断缓存中是否有设置过星座
    var that = this
    var constellationType = app.getValueSync("constellationType")
    if (constellationType){
      var list = this.data.list
      this.setData({
        setType: constellationType,
        setItem: list[constellationType-1]
      },()=>{
        that.getInfo()
      })

    }else{
      var deful = this.data.list[0]
      this.setData({
        setType:1,
        setItem: deful
      }, () => {
        that.getInfo()
      })
      //没有设置过弹窗选择星座
      this.showMode()
    }
    console.log(this.data.setItem)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})